package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.JsCode2Session;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.common.utils.JsCode2SessionUtil;
import com.bigdata.movieticketapi.common.utils.JwtUtil;
import com.bigdata.movieticketapi.entity.User;
import com.bigdata.movieticketapi.mapper.UserMapper;
import com.bigdata.movieticketapi.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.*;
import com.bigdata.movieticketapi.vo.resp.GetUserInfoRespVO;
import com.bigdata.movieticketapi.vo.resp.LoginOrRegRespVO;
import com.bigdata.movieticketapi.vo.resp.PageUniAppUserRespVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 小程序用户表 服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private UserMapper userMapper;


    @SneakyThrows
    @Override
    public LoginOrRegRespVO loginOrReg(LoginOrRegReqVO vo) {
        LoginOrRegRespVO loginOrRegRespVO = new LoginOrRegRespVO();
        JsCode2Session jsCode2Session = JsCode2SessionUtil.getJsCode2Session(vo.getCode());
        User user = query().select("wallet","nick_name","phone_number","avatar", "id").last("limit 1").eq("openid", jsCode2Session.getOpenid()).one();
        if (user == null){
            User insertUser = new User();
            insertUser.setOpenid(jsCode2Session.getOpenid());
            insertUser.setSessionKey(jsCode2Session.getSessionKey());
            insertUser.setNickName("普通用户");
            insertUser.setStatus(0);
            insertUser.setCreateTime(LocalDateTime.now());
            int result = userMapper.insert(insertUser);
            if (result!=1){
                throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
            }
            loginOrRegRespVO.setId(insertUser.getId());
            loginOrRegRespVO.setPhoneNumber(vo.getPhoneNumber());
        }else {
            user.setSessionKey(jsCode2Session.getSessionKey());
            int result = userMapper.updateById(user);
            if (result!=1){
                throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
            }
            loginOrRegRespVO.setId(user.getId());
            loginOrRegRespVO.setNickName(user.getNickName());
            loginOrRegRespVO.setPhoneNumber(user.getPhoneNumber());
            loginOrRegRespVO.setAvatar(user.getAvatar());
            loginOrRegRespVO.setWallet(user.getWallet());
        }
        loginOrRegRespVO.setToken(jwtUtil.createToken(String.valueOf(loginOrRegRespVO.getId()),null));
        return loginOrRegRespVO;
    }

    @Override
    public void updateUserInfo(UpdateUserInfoReqVO vo) {
        User user = userMapper.selectById(vo.getId());
        user.setNickName(vo.getNickName());
        user.setPhoneNumber(vo.getPhoneNumber());
        user.setAvatar(vo.getAvatar());
        int result = userMapper.updateById(user);
        if (result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void adminUpdateUserInfo(AdminUpdateUserInfoReqVO vo) {
        User user = userMapper.selectById(vo.getId());
        System.out.println(vo.getStatus());
        user.setStatus(vo.getStatus());
        user.setWallet(vo.getWallet());
        user.setNickName(vo.getNickName());
        user.setPhoneNumber(vo.getPhoneNumber());
        user.setAvatar(vo.getAvatar());
        int result = userMapper.updateById(user);
        if (result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageUniAppUserRespVO> pageUniAppUser(PageUniAppUserReqVO vo) {
        IPage<PageUniAppUserRespVO> result = userMapper.selectPageUniAppUser(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }

    @Override
    public GetUserInfoRespVO getUserInfo(GetUserInfoReqVO vo) {
        return userMapper.selectUserInfo(vo);
    }
}
